﻿#pragma once
#include <iostream>
#include <vector>
using namespace std;
namespace hash_bucket
{
	//素数表
	inline unsigned long __stl_next_prime(unsigned long n)
	{
		// Note: assumes long is at least 32 bits.
		static const int __stl_num_primes = 28;
		static const unsigned long __stl_prime_list[__stl_num_primes] = {
			53, 97, 193, 389, 769,
			1543, 3079, 6151, 12289, 24593,
			49157, 98317, 196613, 393241, 786433,
			1572869, 3145739, 6291469, 12582917, 25165843,
			50331653, 100663319, 201326611, 402653189, 805306457,
			1610612741, 3221225473, 4294967291
		};
		const unsigned long* first = __stl_prime_list;
		const unsigned long* last = __stl_prime_list + __stl_num_primes;
		const unsigned long* pos = lower_bound(first, last, n);
		return pos == last ? *(last - 1) : *pos;
	}
	template<class T>
	struct keyoft
	{
		const T& operator()(const T& data)
		{
			return data;
		}
	};

	template<class T>
	struct HashFunc
	{
		size_t operator()(const T& data)
		{
			return (size_t)data;
		}
	};


	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}
	};
	template<class K, class T, class KeyOfT, class Hash>
	class HashTable;
	template<class K, class T, class Ptr, class Ref, class KeyOfT, class Hash>
	struct HTIterator
	{
		typedef HashNode<T> Node;
		typedef HTIterator<K, T, Ptr, Ref, KeyOfT, Hash> Self;
		Node* _node;
		const HashTable<K, T, KeyOfT, Hash>* _pht;
		HTIterator(Node* node, const HashTable<K, T, KeyOfT, Hash>* pht)
			:_node(node)
			, _pht(pht)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		bool operator!=(const Self& s)
		{
			return _node != s._node;
		}

		Self& operator++()
		{
			if (_node->_next)
			{
				// 当前桶还有节点
				_node = _node->_next;
			}
			else
			{
				// 当前桶⾛完了，找下⼀个不为空的桶
				KeyOfT kot;
				Hash hs;
				size_t hashi = hs(kot(_node->_data)) % _pht->_tables.size();
				++hashi;
				while (hashi < _pht->_tables.size())
				{
					if (_pht->_tables[hashi])
					{
						break;
					}
					++hashi;
				}
				if (hashi == _pht->_tables.size())
				{
					_node = nullptr; // end()
				}
				else
				{
					_node = _pht->_tables[hashi];
				}
			}
			return *this;
		}
	};

	// K 为 T 中key的类型
	// T 可能是键值对，也可能是K
	// KeyOfT: 从T中提取key
	// Hash将key转化为整形，因为哈市函数使用除留余数法
	template<class K, class T, class KeyOfT = keyoft<int>, class Hash = HashFunc<int>>
	class HashTable
	{
		template<class K, class T, class Ptr, class Ref, class KeyOfT, class Hash>
		friend struct HTIterator;

		typedef HashNode<T> Node;
	public:
		typedef HTIterator<K, T, T*, T&, KeyOfT, Hash> Iterator;
		typedef HTIterator<K, T, const T*, const T&, KeyOfT, Hash> ConstIterator;

		Iterator Begin()
		{
			if (_n == 0)
				return End();
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
				{
					return Iterator(cur, this);
				}
			}
			return End();
		}

		Iterator End()
		{
			return Iterator(nullptr, this);
		}

		ConstIterator Begin() const
		{
			if (_n == 0)
				return End();
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				if (cur)
				{
					return ConstIterator(cur, this);
				}
			}
			return End();
		}

		ConstIterator End() const
		{
			return ConstIterator(nullptr, this);
		}

		HashTable()
		{
			_tables.resize(__stl_next_prime(0));
		}

		// 哈希桶的销毁
		~HashTable()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
		}

		HashTable(const HashTable& HT)
		{
			_tables.resize(__stl_next_prime(0));
			for (size_t i = 0; i < HT._tables.size(); i++)
			{
				Node* cur = HT._tables[i];
				while (cur)
				{
					Insert(cur->_data);
					cur = cur->_next;
				}
			}
		}

		//s1 = s2
		void operator=(HashTable HT)
		{
			_tables.swap(HT._tables);
			_n = HT._n;
		}

		// 插入值为data的元素，如果data存在则不插入
		pair<Iterator,bool> Insert(const T& data)
		{
			KeyOfT kot;
			Hash hash;
			//去重
			Iterator ret = Find(kot(data));
			if (ret != End())
				return {ret, false};
				// 负载因子 == 1时扩容
			if (_n == _tables.size())
			{
				vector<Node*> NewTable(__stl_next_prime(_tables.size() + 1));
				for (size_t i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t Hashi = hash(kot(cur->_data)) % NewTable.size();
						cur->_next = NewTable[Hashi];
						NewTable[Hashi] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(NewTable);
			}
			//插入过程
			Node* NewNode = new Node(data);
			size_t Hashi = hash(kot(data)) % _tables.size();
			NewNode->_next = _tables[Hashi];
			_tables[Hashi] = NewNode;
			++_n;
			return { Iterator(_tables[Hashi], this) , true};
		}

		// 在哈希桶中查找值为key的元素，存在返回true否则返回false﻿
		Iterator Find(const K& key)
		{
			KeyOfT kot;
			Hash hash;
			size_t Hashi = hash(key) % _tables.size();
			Node* cur = _tables[Hashi];
			while (cur)
			{
				if (hash(kot(cur->_data)) == key)
				{
					return Iterator(cur,this);
				}
				cur = cur->_next;
			}
			return End();
		}

		// 哈希桶中删除key的元素，删除成功返回true，否则返回false
		bool Erase(const K& key)
		{
			KeyOfT kot;
			Hash hash;
			size_t Hashi = hash(key) % _tables.size();
			Node* cur = _tables[Hashi];
			Node* prev = nullptr;
			while (cur)
			{
				if (hash(kot(cur->_data)) == key)
				{
					if (prev)
					{
						prev->_next = cur->_next;
					}
					else
					{
						_tables[Hashi] = nullptr;
					}
					delete cur;
					--_n;
					return true;
				}
				prev = cur;
				cur = cur->_next;
			}
			return false;
		}
	private:
		vector<Node*> _tables;  // 指针数组
		size_t _n = 0;			// 表中存储数据个数
	};
}